﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YJKRevitAxisModule
{
	public partial class GridTagShowHideFrm : System.Windows.Forms.Form
	{
		public GridTagShowHideFrm(ExternalCommandData commandData)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.m_commandData = commandData;
			this.InitializeComponent();
			this.ReadCheckStatus();
		}

		public int CommentChecked
		{
			get
			{
				if (this.rdbCharact.Checked)
				{
					return 1;
				}
				return 2;
			}
			set
			{
				if (value == 1)
				{
					this.rdbCharact.Checked = true;
					return;
				}
				this.rdbcircleandchar.Checked = true;
			}
		}

		public int RangeChecked
		{
			get
			{
				if (this.rdbcurrentview.Checked)
				{
					return 1;
				}
				return 2;
			}
			set
			{
				if (value == 1)
				{
					this.rdbcurrentview.Checked = true;
					return;
				}
				this.rdballview.Checked = true;
			}
		}

		public int SelMode
		{
			get
			{
				if (this.rdbpoint.Checked)
				{
					return 1;
				}
				return 2;
			}
			set
			{
				if (value == 1)
				{
					this.rdbpoint.Checked = true;
					this.rdbractangle.Checked = false;
					return;
				}
				this.rdbractangle.Checked = true;
				this.rdbpoint.Checked = false;
			}
		}

		private bool SaveCheckStatus()
		{
			string path = Path.GetTempPath() + "GridBubbleShowHide.dat";
			try
			{
				FileStream fileStream = new FileStream(path, FileMode.Create);
				StreamWriter streamWriter = new StreamWriter(fileStream);
				streamWriter.WriteLine(base.Location.X);
				streamWriter.WriteLine(base.Location.Y);
				streamWriter.WriteLine(this.CommentChecked);
				streamWriter.WriteLine(this.RangeChecked);
				streamWriter.WriteLine(this.SelMode);
				streamWriter.Flush();
				streamWriter.Close();
				fileStream.Close();
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private bool ReadCheckStatus()
		{
			string path = Path.GetTempPath() + "GridBubbleShowHide.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				string s = streamReader.ReadLine();
				this.CommentChecked = int.Parse(s);
				s = streamReader.ReadLine();
				this.RangeChecked = int.Parse(s);
				s = streamReader.ReadLine();
				this.SelMode = int.Parse(s);
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void GridTagShowHideFrm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				GridTagShowHideFrm.SendMessage(CmdGridTagShowHide._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void btn_cancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public void PressEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			GridTagShowHideFrm.SendMessage(CmdGridTagShowHide._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			GridTagShowHideFrm.keybd_event((byte)bVk, 0, dwFlags, 0);
			GridTagShowHideFrm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void rdbpoint_CheckedChanged(object sender, EventArgs e)
		{
			this.m_clickModeBtn = true;
			this.PressEsc();
		}

		private void GridTagShowHideFrm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveCheckStatus();
		}

		private void GridTagShowHideFrm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.PressEsc();
			this.PressEsc();
		}

		private void GridTagShowHideFrm_Load(object sender, EventArgs e)
		{
			this.label1.Hide();
		}

		private ExternalCommandData m_commandData;

		public bool m_clickModeBtn;
	}
}
